<?php
class ControllerDelimanThongkeluongbaoton extends Controller
{
	private $error = array();
	
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		
	 	$this->load->model("deliman/kybao");
		$this->load->model("deliman/loaibao");
		$this->load->model("deliman/khuvuc");
		$this->load->model("deliman/nhanvien");
		$this->load->model("deliman/khachhang");
		$this->load->model("deliman/phieugiaobao");
		$this->load->model("deliman/phieutrabao");
		$this->load->model("common/control");
   	}
	
	public function index()
	{
		$this->getList();
	}
	
	
	//Ham lay thong tin ky bao
	public function getkybao()
	{		
		$loaibaoid = $this->request->get['loaibaoid'];
		$select =  $this->request->get['select'];
		
		$where = "";
		$where .= " AND loaibaoid = '".$loaibaoid."'";
		$where .= " ORDER BY ngayphathanh DESC";
		
		$kybaos = $this->model_deliman_kybao->getList($where);
		$cb = $this->model_common_control->getDataCombobox($kybaos,"tenkybao","kybaoid",$select);
		$this->data['output'] = '<option value="">Chọn tất cả</option>'.$cb;
		
		$this->template='common/output.tpl';
		$this->render();
	}
	
	//Lay khu vuc phu trach cua nv dang dang nhan
	function getKhuVucQuanLy()
	{
		$nhanvienid = $this->user->getNhanVien();
		$arr_khuvucquanlyid = array();
		if($nhanvienid)
		{
			$where = " AND nhanvienphutrachid = '".$nhanvienid."'";
			$data_khuvuc = $this->model_deliman_khuvuc->getList($where);
			$arr_khuvucquanlyid = $this->string->matrixToArray($data_khuvuc,'khuvucid');
		}
		else
		{
			$arr_khuvucquanlyid[] = 0;	
		}
		/*foreach($data_khuvuc as $key => $khuvuc)
		{
			$data_khuvuc[$key]['tenkhuvuc']	.= " ".$khuvuc['tenphuongxa']." ".$khuvuc['tenquanhuyen']." ".$khuvuc['tentinhthanh'];
		}*/
		$data_khuvuc = array();
		foreach($arr_khuvucquanlyid as $khuvucid)
		{
			$data = array();
			$this->model_deliman_khuvuc->getTree($khuvucid,$data,$khuvucid);
			$data_khuvuc = array_merge($data_khuvuc,$data);
		}
		return $data_khuvuc;
	}
	public function getCBNhanVienGaoBao()
	{
		/*$khuvucid = $this->request->get['khuvucid'];
		$data_khuvuc = array();
		$this->model_deliman_khuvuc->getTree($khuvucid,$data_khuvuc,$khuvucid);
		$arr_khuvucid = $this->string->matrixToArray($data_khuvuc,'khuvucid');
		$where = " AND khuvucid in ('".implode("','",$arr_khuvucid)."') AND tinhtrang <> 'deleted'";
		
		$data_nhanvienphutrach = $this->model_deliman_nhanvien->getPhuTrachKhuVucList($where);
		$arr_nhanvienphutrachid = $this->string->matrixToArray($data_nhanvienphutrach,'nhanvienphutrachid');
		
		$where = " AND loainhanvienid = '3'";
		$where .= " AND nhanvienid in ('". implode("','",$arr_nhanvienphutrachid) ."')";
		$data_nhanvien = $this->model_deliman_nhanvien->getList($where);
		
		$this->data['output'] = '<option value=""></option>'; 
		$this->data['output'] .=  $this->model_common_control->getDataCombobox($data_nhanvien, 'hoten', 'nhanvienid');*/
		//lay id nhan vien quan ly khu vuc khi dang nhap vao he thong
		$nhanvienphancongid = $this->user->getNhanVien();
		
		$khuvucid = $this->request->get['khuvucid'];
		$arr_khuvucid = array();
		if($khuvucid == "")
		{
			//lay khu vuc no ql len
			$where = " AND nhanvienphutrachid = '".$nhanvienphancongid."'";
			$data_khuvuc = $this->model_deliman_khuvuc->getList($where);
			$arr_khuvucid = $this->string->matrixToArray($data_khuvuc,'khuvucid');
		}
		else
		{
			$arr_khuvucid[] = $khuvucid;
		}
		//$select =  $this->request->get['select'];
		$khuvus = array();
		foreach($arr_khuvucid as $khuvucid)
		{
			$data_temp = array();
			$this->model_deliman_khuvuc->getTree($khuvucid, $data_temp);
			$khuvus = array_merge_recursive($khuvus,$data_temp);
		}
		
		$arrkhuvucid = $this->string->matrixToArray($khuvus,'khuvucid');
		$where = " AND khuvucid in ('".implode("','",$arrkhuvucid)."') AND tinhtrang <> 'deleted'";
	
		$phutrackhuvucs = $this->model_deliman_nhanvien->getPhuTrachKhuVucList($where);
		$listnhanvienphutrachid = $this->string->matrixToArray($phutrackhuvucs,'nhanvienphutrachid');
		//print_r($listnhanvienphutrachid);
		$where = " AND nhanvienid in ('".implode("','",$listnhanvienphutrachid)."')";
		
	/*	$arr_phutrachkhachhang = $this->model_deliman_nhanvien->getPhuTrachKhachHangList(" AND nhanvienphancongid = '".$nhanvienphancongid."'");
		//lay nhung id cua nhan vien phu trach khach hang trong table phu trach khach hang
		$arr_ptkhnhanvienphutrachid = $this->string->matrixToArray($arr_phutrachkhachhang,'nhanvienphutrachid');*/
		
		//$where = "AND nhanvienid in ('".implode("','",$arr_ptkhnhanvienphutrachid)."')";
		
		$where .= " AND loainhanvienid = 3";
		$nhanviens = $this->model_deliman_nhanvien->getList($where);
		
		$this->data['output'] = $this->model_common_control->getDataCombobox($nhanviens, "hoten", "nhanvienid", $select);
		
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();	
	}
	
	private function getList()
	{
		$data = $this->request->get;
		
		//
		$this->data['data_khuvucquanly'] = $this->getKhuVucQuanLy();
		
		//$this->data['cbKhuVucQuanLy'] = $this->model_common_control->getDataCombobox($data_khuvucquanly,"tenkhuvuc",'khuvucid',"");
		//dieu kien tim kiem
		if(trim($data['tungay'])!='')
		{
			$where .= " AND ngayphathanh  >= '".$this->date->formatViewDate($data['tungay'])."'";
		}
		if(trim($data['denngay'])!='')
		{
			$where .= " AND ngayphathanh  <= '".$this->date->formatViewDate($data['denngay'])."'";
		}
		if(trim($data['loaibaoid'])!='')
		{
			$where .= " AND loaibaoid = '".$data['loaibaoid']."'";
		}
		if(trim($data['kybaoid'])!='')
		{			
			//Danh sach ky bao ma khach hang da dat
			$where .= " AND kybaoid = '".$data['kybaoid']."'";			
		}
		
		
		//lay du lieu loai bao do vao combobox loai bao
		$danhsachthongtindatbao=$this->model_deliman_loaibao->getListThongtinDatBao();
		$arr_loaibaoid=$this->string->matrixToArray($danhsachthongtindatbao,'loaibaoid');
		$where_loaibao=" AND loaibaoid in ('".implode("','",$arr_loaibaoid)."')";
		
		
		//$this->data['loaibaos'] = $this->model_deliman_loaibao->getList($where_loaibao);
		$this->data['loaibaos'] = $this->model_deliman_loaibao->getList();
		$this->data['loaibao_combo'] = $this->model_deliman_loaibao->getList();
		$this->data['loaibao'] = $this->model_common_control->getDataCombobox($this->data['loaibao_combo'], "tenloaibao", "loaibaoid", $select);
		
		//print_r($loaibaos);
		
		$this->data['insert'] = $this->url->http('deliman/kybao/insert');
		$this->data['delete'] = $this->url->http('deliman/kybao/delete');
		
		$this->data['datas'] = array();
		$nhanviengiaobaoid = $data['nhanviengiaobaoid'];
		//
		if($nhanviengiaobaoid)
		{
			$where .= " ORDER BY ngayphathanh DESC";
			$rows = $this->model_deliman_kybao->getList($where);
			//Page
			$page = $this->request->get['page'];		
			$x=$page;		
			$limit = 20;
			$total = count($rows); 
			// work out the pager values 
			$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
			
			$pager  = $this->pager->getPagerData($total, $limit, $page); 
			$offset = $pager->offset; 
			$limit  = $pager->limit; 
			$page   = $pager->page;
			for($i=0;$i < count($rows[$i]);$i++)
			//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
			{
				$this->data['datas'][$i] = $rows[$i];
				$this->data['datas'][$i]['link_edit'] = $this->url->http('deliman/kybao/update&kybaoid='.$this->data['datas'][$i]['kybaoid']);
				$this->data['datas'][$i]['text_edit'] = "Edit";
				
				$loaibao = $this->model_deliman_loaibao->getItem($this->data['datas'][$i]['loaibaoid']);
				$this->data['datas'][$i]['tenloaibao'] = $loaibao['tenloaibao'];
				
				//$where_kybao=" AND kybaoid='".$this->data['datas'][$i]['kybaoid']."' AND trangthai like 'dagiao%'";
				//$thongtindatbao = $this->model_deliman_khachhang->sumSoLuongGiaoBao($where_kybao);
				//print_r($thongtindatbao);
				//$this->data['datas'][$i]['thucgiao']=$thongtindatbao['thucgiao'];
				
				//Lay so luong bao nhan tu nhan vien quan ly
				$where = " AND nhanviengiaobaoid = '".$nhanviengiaobaoid."'";
				$where .= " AND kybaoid = '".$this->data['datas'][$i]['kybaoid']."'";
				$data_ctphieugiao = $this->model_deliman_phieugiaobao->getChiTietPhieuGiaoBaoList($where);
				
				$tongbaoduocnhan = 0;
				foreach($data_ctphieugiao as $ct)
					$tongbaoduocnhan += $ct['soluong'];
				$this->data['datas'][$i]['tongbaoduocnhan'] = $tongbaoduocnhan;
				//So luong bao tra cho nhan vien quan ly
				$where = " AND nhanvientraid = '".$nhanviengiaobaoid."'";
				$where .= " AND kybaoid = '".$this->data['datas'][$i]['kybaoid']."'";
				$data_ctphieutra = $this->model_deliman_phieutrabao->getChiTietPhieuTraBaoList($where);
				$tongbaotra = 0;
				foreach($data_ctphieutra as $ct)
					$tongbaotra += $ct['soluongtra'];
				$this->data['datas'][$i]['tongbaotra'] = $tongbaotra;
				//So luong bao da giao khach hang
				$where = " AND 	nhanviengiaoid = '".$nhanviengiaobaoid."'";
				$where .= " AND kybaoid = '".$this->data['datas'][$i]['kybaoid']."'";
				$data_thongtindatbao = $this->model_deliman_khachhang->getThongTinDatBaoList($where);
				$tongbaodagiao = 0;
				foreach($data_thongtindatbao as $ct)
					$tongbaodagiao += $ct['soluongdagiao'];
				$this->data['datas'][$i]['tongbaodagiao'] = $tongbaodagiao;
				//Tinh so luong bao ton
				$this->data['datas'][$i]['soluongton'] = $tongbaoduocnhan - $tongbaotra - $tongbaodagiao;
				
			}
		}
		
		
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/thongkeluongbaoton.tpl";
		
		if($this->request->get['print'] == true)
		{
			$this->layout="layout/print";
		}
		else
		{
			$this->layout="layout/deliman";
		}
		
		
		$this->render();
	}
}
?>